
***inputs:
* $OrigData\ISD`y'.csv y=2013:2018
* $OrigData\NEWISD`y'.csv y=2013:2018


*** outputs:
* $Data\Weather_ISD`y'.csv y=2013:2018
* $Data\Weather_NEWISD`y'.dta


forval y = 2013(1)2018 {
	clear
	
	insheet using $OrigData/ISD`y'.csv, 

	drop v1	
	
	rename wban WBAN
	rename usaf USAF
	
	
	rename date date_num
	tostring date_num, gen(date_str) usedisplayformat 

	gen date = date(date_str, "YMD")
	format date %td

	tostring time , gen(time_str) 

	gen hour_str = substr(time_str, -4,2)
	replace hour_str = substr(time_str, -3,1) if hour_str==""
	replace hour_str = "0" if length(time_str) <=2 
	destring hour_str, gen(hour)
	
	gen min_str = substr(time_str,-2,2)
	replace min_str = substr(time_str,-1,1) if length(time_str) <2
	
	destring min_str, gen(min)
	
	
	gen hour_round = (hour+1)*(min>30) + hour*(min<=30)
	replace hour_round = 0 if hour_round ==24
	
	drop time hour_str min_str min hour date_num
	
	rename hour_round hour
	
	replace temperature =. if temperature ==9999
	replace wind_speed =. if wind_speed ==9999
	replace wind_direction =. if wind_direction ==999
save $Data/Weather_ISD`y'.dta, replace
}




forval y = 2013(1)2018 {
	clear
	
	insheet using $OrigData/NEWISD`y'.csv, 

	drop v1	
	
	rename wban WBAN
	rename usaf USAF
	
	
	rename date date_num
	tostring date_num, gen(date_str) usedisplayformat 

	gen date = date(date_str, "YMD")
	format date %td

	tostring time , gen(time_str) 

	gen hour_str = substr(time_str, -4,2)
	replace hour_str = substr(time_str, -3,1) if hour_str==""
	replace hour_str = "0" if length(time_str) <=2 
	destring hour_str, gen(hour)
	
	gen min_str = substr(time_str,-2,2)
	replace min_str = substr(time_str,-1,1) if length(time_str) <2
	
	destring min_str, gen(min)
	
	
	gen hour_round = (hour+1)*(min>30) + hour*(min<=30)
	replace hour_round = 0 if hour_round ==24
	
	drop time hour_str min_str min hour date_num
	
	rename hour_round hour
	
	replace temperature =. if temperature ==9999
	replace wind_speed =. if wind_speed ==9999
	replace wind_direction =. if wind_direction ==999
	
	
save $Data/Weather_NEWISD`y'.dta, replace
}
